package com.hebeu.gfh.utils;


import com.hebeu.gfh.exception.AppExceptionCodeMsg;

/**
 *
 * @description 服务器响应工具类
 * @author Liujianli
 * @date 2023-02-27
 */
public class R <T>{

    private int code = 200; //返回状态码
    private String msg =  "success" ; //返回的message
    private T data; //返回的数据

    //全参构造函数
    public R(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
    //无参构造函数
    public R() {
    }

    public int getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }

    public static <T> R <T> ok(){
        R result = new R<T>(200, "success", null);
        return result;
    }

    //成功时传入data数据
    public static <T> R <T> ok(T data){
        R result = new R(200, "success", data);
        return result;
    }

    //成功时传入data和msg
    public static <T> R ok (String msg ,T data){
        R result = new R(200, msg, data);
        return result;
    }

    public static <T> R error(AppExceptionCodeMsg exceptionCodeMsg){
        R result = new R(exceptionCodeMsg.getCode(),exceptionCodeMsg.getMsg(),null);
        return result;
    }

    //失败时传入code和msg
    public static <T> R error(int code ,String msg){
        R result = new R(code, msg, null);
        return result;
    }

}
